package com.block.risk.service.type.item;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.block.risk.model.result.RiskJudgeResult;
import com.block.risk.service.IDefaultItemService;
import org.springframework.stereotype.Service;

@Service
public class BiaoLuoItemService implements IDefaultItemService {
    @Override
    public RiskJudgeResult getControlResult(JSONObject resultDetial) {

        /**
         * 社保公积金营业执照三选一  芝麻分600+
         * WD正常履约＞80%  无当前YQ  近一个月要有履约 不要白户
         * 地区屏蔽甘肃 上海 湖南 宁夏  广西 西藏
         *
         *
         * 还款率85%，近3个月贷款笔数至少3笔，贷款已结清订单数至少5笔，近一月有履约；屏蔽上海 甘肃 宁夏 西藏 广西 ；近三个月还款失败笔数＜还款成功笔数
         *
         */

        RiskJudgeResult riskJudgeResult = new RiskJudgeResult();
        riskJudgeResult.setPass(false);

        JSONObject currentReportDetail = resultDetial.getJSONObject("current_report_detail");
        JSONObject behaviorReportDetail = resultDetial.getJSONObject("behavior_report_detail");
        JSONObject applyReportDetail = resultDetial.getJSONObject("apply_report_detail");

        if (ObjectUtil.isEmpty(behaviorReportDetail)){
            riskJudgeResult.setResult("行为数据结果为空；【behavior_report_detail】");
            return riskJudgeResult;
        }

        // 正常还款订单数占贷款总订单数比例
        String numberRate = behaviorReportDetail.getString("B22170034");
        numberRate = numberRate.replace("%", "");
        if (Integer.valueOf(numberRate) < 85){
            riskJudgeResult.setResult(StrUtil.format("订单比例85以上不通过；【{}】", numberRate));
            return riskJudgeResult;
        }

        //近 3 个月货款笔数
        String threeLoanNumber = behaviorReportDetail.getString("B22170034");
        if (Integer.valueOf(threeLoanNumber) < 3){
            riskJudgeResult.setResult(StrUtil.format("近 3 个月货款笔数3笔以上不通过；【{}】", numberRate));
            return riskJudgeResult;
        }

        // 近3 个月履约贷款次数
        String threeRepayNumber = behaviorReportDetail.getString("B22170046");
        if (Integer.valueOf(threeRepayNumber) < 5){
            riskJudgeResult.setResult(StrUtil.format("近3 个月履约贷款次数5笔以上不通过；【{}】", numberRate));
            return riskJudgeResult;
        }

        // B22170045 近1 个月履约贷 款次数
        String repayNumber = behaviorReportDetail.getString("B22170045");
        if (Integer.valueOf(repayNumber) < 1){
            riskJudgeResult.setResult(StrUtil.format("近1 个月履约贷 款次数1笔以上不通过；【{}】", repayNumber));
            return riskJudgeResult;
        }

        // 近6个月逾期M0 = 0
        String numberSixM0 = behaviorReportDetail.getString("B22170025");
        if (Integer.valueOf(numberSixM0) > 0){
            riskJudgeResult.setResult(StrUtil.format("近6个月逾期M0不通过；【{}】", numberSixM0));
            return riskJudgeResult;
        }

        riskJudgeResult.setPass(true);
        riskJudgeResult.setResult("全景分控通过");
        return riskJudgeResult;
    }
}
